package ex;

import com.egghiro.framework.j2se.game.core.timer.SystemTimer;

public final class GameContext {

    public static int nextContextID = 0;

    private ThreadGroup threadGroup;

    private final IView view;

    private SystemTimer timer;

    public GameContext(IView view, SystemTimer timer) {
        this.view = view;
        this.timer = timer;
    }

    public ThreadGroup getThreadGroup() {
        if (threadGroup == null || threadGroup.isDestroyed()) {
            threadGroup = new ThreadGroup("LGame-View" + nextContextID);
            nextContextID++;
        }
        return threadGroup;
    }

    public Thread createThread(Runnable runnable) {
        while (true) {
            ThreadGroup currentGroup = getThreadGroup();
            synchronized (currentGroup) {
                if (getThreadGroup() != currentGroup) {
                    continue;
                }
                Thread thread = new Thread(currentGroup, runnable, "LGame-View"
                        + nextContextID);
                return thread;
            }
        }
    }

    public void setAnimationThread(Thread thread) {
        if (thread != null) {
            if (threadGroup == null
                    || !threadGroup.parentOf(thread.getThreadGroup())) {
                threadGroup = thread.getThreadGroup();
            }
        }
    }

    public SystemTimer getTimer() {
        return timer;
    }

    public IView getView() {
        return view;
    }

}
